
import Utils from '@/utils/util'
const dateFun = {
  init: function() {
    const that = this
    that.Date = new Date()
  },
  getCurrentYear: function() {
    return new Date().getFullYear()
  },
  getCurrentMonth: function() {
    return new Date().getMonth() + 1
  },
  getCurrentDay: function() {
    return new Date().getDate()
  },
  getCurrentWeek: function(index) {
    let weekName
    const weekIndex = new Date().getDay() // 获取 今天的 星期index，0-6(星期,0 是星期天)
    const new_index = (weekIndex + index) % 7
    switch (new_index) {
      case 0:
        weekName = '星期天'
        break
      case 1:
        weekName = '星期一'
        break
      case 2:
        weekName = '星期二'
        break
      case 3:
        weekName = '星期三'
        break
      case 4:
        weekName = '星期四'
        break
      case 5:
        weekName = '星期五'
        break
      case 6:
        weekName = '星期六'
        break
    }
    return weekName
  },
  getCurrentZeroStamp: function() {
    // 今天0时时间戳
    const nowZeroDate = new Date()
    const nowZeroStr = Utils.formatTime(nowZeroDate, 'Y-M-D 0:0:0')
    // console.log('nowZeroDate', nowZeroDate, 'nowZeroStr', nowZeroStr)
    const tranNowZeroDate = Utils.formatTranNumber(nowZeroStr)
    // console.log('tranNowZeroDate', tranNowZeroDate)
    return tranNowZeroDate
  },
  getNowTimeStamp: function() {
    // 现在时间戳
    const nowDate = new Date()
    const nowStr = Utils.formatTime(nowDate, 'Y-M-D h:m:s')
    // console.log('nowDate', nowDate, 'nowStr', nowStr)
    const tranNowDate = Utils.formatTranNumber(nowStr)
    // console.log('tranNowDate', tranNowDate)
    return tranNowDate
  },
  getCurrentMonthDayNum: function() {
		    const year = dateFun.getCurrentYear()
		    const month = dateFun.getCurrentMonth()
		    const dayNum = new Date(year, month, 0)
		    return dayNum.getDate()
  },
  juedgYear: function(_month) {
    const month = dateFun.getCurrentMonth()
    return _month >= month
  },
  dateRst: function(arr, _el, _boolen) {
    const isThisYear = dateFun.juedgYear(arr[0].id)
    const thisYear = dateFun.getCurrentYear()
    const nextYear = thisYear + 1
    let startTime = ''
    let startS = ''
    let endS = ''
    let endTime = ''
    let timeLine = ''
    let fillDate = ''
    // console.log('arr', arr, 'arr[0]', arr[0], 'arr[1]', arr[1], 'arr[2]', arr[2])
    switch (arr[2].id) {
      case 1:
        startS = '00'
        endS = '01:00'
        break
      case 2:
        startS = '01'
        endS = '02:00'
        break
      case 3:
        startS = '02'
        endS = '03:00'
        break
      case 4:
        startS = '03'
        endS = '04:00'
        break
      case 5:
        startS = '04'
        endS = '05:00'
        break
      case 6:
        startS = '05'
        endS = '06:00'
        break
      case 7:
        startS = '06'
        endS = '07:00'
        break
      case 8:
        startS = '07'
        endS = '08:00'
        break
      case 9:
        startS = '08'
        endS = '09:00'
        break
      case 10:
        startS = '09'
        endS = '10:00'
        break
      case 11:
        startS = '10'
        endS = '11:00'
        break
      case 12:
        startS = '11'
        endS = '12:00'
        break
      case 13:
        startS = '12'
        endS = '13:00'
        break
      case 14:
        startS = '13'
        endS = '14:00'
        break
      case 15:
        startS = '14'
        endS = '15:00'
        break
      case 16:
        startS = '15'
        endS = '16:00'
        break
      case 17:
        startS = '16'
        endS = '17:00'
        break
      case 18:
        startS = '17'
        endS = '18:00'
        break
      case 19:
        startS = '18'
        endS = '19:00'
        break
      case 20:
        startS = '19'
        endS = '20:00'
        break
      case 21:
        startS = '20'
        endS = '21:00'
        break
      case 22:
        startS = '21'
        endS = '22:00'
        break
      case 23:
        startS = '22'
        endS = '23:00'
        break
      case 24:
        startS = '23'
        endS = '24:00'
        break
    }
    const str1 = arr[1].value
    const reg1 = new RegExp('点') // 创建正则表达式对象
    let r_str1 = str1.replace(reg1, '') // 把'is'替换为空字符串
    const str2 = arr[2].value
    const reg2 = new RegExp('分') // 创建正则表达式对象
    let r_str2 = str2.replace(reg2, '') // 把'is'替换为空字符串
    const str3 = arr[0].value
    const r_str3_index = str3.indexOf('月')
    const r_str3 = str3.substring(0, r_str3_index)
    // console.log('r_str3_index', r_str3_index, 'r_str3', r_str3)
    if (r_str1 === '0') {
      r_str1 = '00'
    }
    if (r_str2 === '0') {
      r_str2 = '00'
    }
    if (isThisYear) {
      startTime = thisYear + '-' + arr[0].id + '-' + arr[1].id + ' ' + startS
      endTime = thisYear + '-' + arr[0].id + '-' + arr[1].id + ' ' + endS
      fillDate = thisYear + '-' + r_str3 + '-' + arr[0].id + ' ' + r_str1 + ':' + r_str2
    } else {
      startTime = nextYear + '-' + arr[0].id + '-' + arr[1].id + ' ' + startS
      endTime = nextYear + '-' + arr[0].id + '-' + arr[1].id + ' ' + endS
      fillDate = thisYear + '-' + r_str3 + '-' + arr[0].id + ' ' + r_str1 + ':' + r_str2
    }
    timeLine = new Date(startTime).getTime() / 1000 + ',' + new Date(endTime).getTime() / 1000
			  // 回填参数
    if (_el && !_boolen) {
      document.getElementById(_el).value = fillDate
    } else if (_boolen) {
      return {
        // 'timeLine': timeLine,
        'fillDate': fillDate
      }
    } else {
      return {
        // 'timeLine': timeLine,
        'fillDate': fillDate
      }
    }
    // 保存参数
    return timeLine/* 时间段转化为时间戳字符串*/
  },

  currentDateRst: function() {
    const getDateArr = dateFun.getDateArr(0)
    const currentDate = getDateArr[0]
    const fillCurrentDate = []
    // console.log('currentDateRst-getDateArr', getDateArr, 'currentDate', currentDate)
    fillCurrentDate.push({
      id: currentDate.month,
      value: currentDate.month
    })
    fillCurrentDate.push({
      id: currentDate.child[0],
      value: currentDate.child[0]
    })
    fillCurrentDate.push({
      id: currentDate.minutesSection,
      value: currentDate.minutesSection
    })
    // console.log('currentDateRst-fillCurrentDate', fillCurrentDate)
    return dateFun.dateRst(fillCurrentDate)
  },
  /*
		*获取N天后的时间
		**/
  getDateArr: function(AddDayCount) {
    let dd = ''
    const today = dateFun.getCurrentDay()
    const toDayZeroStamp = dateFun.getCurrentZeroStamp()
    const nowTimeStamp = dateFun.getNowTimeStamp()
    const subDateSlice = (nowTimeStamp - toDayZeroStamp) / 3600000
    // 当前时间戳 - 今日0点时间戳 的结果除以 1小时单位的时间戳，得出现在 已有几个小时
    const dateList = []
    let index = 0
    if (subDateSlice > 15) {
      index = 0
      AddDayCount = AddDayCount - 1
    } else {
      AddDayCount = AddDayCount - 1
    }
    for (let i = index; i <= AddDayCount; i++) {
      // i 控制天数 ， 0 今天 ，1 明天， 2后天
      dd = new Date().setDate(today + i)// 获取AddDayCount天后的日期
      const y = new Date(dd).getFullYear()
      const m = new Date(dd).getMonth() + 1// 获取当前月份的日期
      const d = new Date(dd).getDate()
      const c = dateFun.timeSliceFun(i)
      // console.log('AddDayCount', AddDayCount, 'dd', dd, 'i', i, 'd', d)
      // 数据存储
      dateList.push({
        year: y,
        month: m,
        day: d,
        child: c
      })
    }
    return dateList
  },

  /*
		*月展示参数
		*/
  monthJsonOp: function(_mval, _dval, _child, dix) {
    // console.log('monthJsonOp-mavl', _mval, '_dval', _dval, '_child', _child, 'dix', dix)
    const child = []
    for (let h = 0; h < _child.length; h++) {
      const hoursOp = dateFun.hoursJsonOp(_child, _child[h])
      child.push(hoursOp)
    }
    const toDayNum = dateFun.getCurrentDay()
    // const addDayNum = dix + 1 // dix 传入的往后的天数 是 0 1 2
    // console.log('monthJsonOp()-dix', dix)
    const weekName = dateFun.getCurrentWeek(dix)
    if (toDayNum === _dval) {
      return {
        'id': _dval,
        'value': _mval + '月' + _dval + '日' + '今天',
        'arr': dix,
        'child': child
      }
    } else {
      return {
        'id': _dval,
        'arr': dix,
        'value': _mval + '月' + _dval + '日' + weekName,
        'child': child
      }
    }
  },

  /*
		*日展示参数 -目前展示 hours
    */
  dayJsonOp: function(_mval, _dval, _child) {
    return {
      'id': _dval,
      'value': _dval,
      'child': _child
    }
  },

  /*
		* hours 参数展示
    */
  hoursJsonOp: function(_child, _child_item) {
    const child = []
    for (let h = 0; h < _child_item.child.length; h++) {
      const minutesOp = dateFun.minutesJsonOp(_child_item.child[h])
      child.push(minutesOp)
    }
    return {
      'id': _child_item.id,
      'value': _child_item.value + '点',
      'child': child
    }
  },

  /*
		* minutes 参数展示
    */
  minutesJsonOp: function(_minutes) {
    return {
      'id': _minutes.id,
      'value': _minutes.value + '分'
    }
  },

  /*
		* minutes 参数展示
    */
  halfHoursFun: function(index) {
    const minutesList = []
    const c = index
    let minutes_item = {}
    // 目前 index=0 展示8:00 ; index=1 展示8：30
    if (c === 1) {
      for (let y = 0; y < 2; y++) {
        if (y === 0) {
          minutes_item = {
            id: 1,
            value: 1 * 30
          }
        } else {
          minutes_item = {
            id: 0,
            value: 0 * 30
          }
        }
        minutesList.push(minutes_item)
      }
    } else {
      for (let y = 0; y < 2; y++) {
        minutes_item = {
          id: y,
          value: y * 30
        }
        minutesList.push(minutes_item)
      }
    }
    // console.log('index', index, 'halfHoursFun-minutesList', minutesList)
    return minutesList
  },
  /*
  * 分钟段创造参数展示
  */
  minutesSectionFun: function(index) {
    // 今天零时
    const toDayZeroStamp = dateFun.getCurrentZeroStamp()
    const nowTimeStamp = dateFun.getNowTimeStamp()
    // 判断时间段
    // 初始化符合条件的时间段
    const minutesList = []
    const c_index = index
    const subDateSlice = (nowTimeStamp - toDayZeroStamp) / 3600000

    let halfhour = false
    const cutNum = subDateSlice + 0.5 - Number.parseInt(subDateSlice)
    if (cutNum === 0) {
      halfhour = false
    } else if (cutNum > 0 && cutNum <= 0.75) {
      halfhour = true
    } else if (cutNum > 0.75 && cutNum <= 1.25) {
      halfhour = false
    } else if (cutNum > 1.25) {
      halfhour = true
    }
    if (c_index === 0) {
      // 分钟区域 从 00分 开始展示 或 从 30分 开始展示
      // console.log('halfhour-math', halfhour)
      if (halfhour) {
        for (let c = 3; c < 6; c++) {
          const minutes_item2 = {
            id: c,
            value: c * 10
          }
          minutesList.push(minutes_item2)
        }
      } else {
        for (let c = 0; c < 6; c++) {
          const minutes_item2 = {
            id: c,
            value: c * 10
          }
          minutesList.push(minutes_item2)
        }
      }
    }
    // console.log('minutesSectionFun-c_index', c_index, 'cutNum', cutNum, 'minutesList', minutesList)
    return minutesList
  },
  /*
		*时间段参数展示
		*/
  timeSliceFun: function(index) {
    // 今天零时
    const toDayZeroStamp = dateFun.getCurrentZeroStamp()
    const nowTimeStamp = dateFun.getNowTimeStamp()
    // console.log('minutesSectionFun-nowTimeStamp', nowTimeStamp)
    // 初始化符合条件的时间段
    const timeSlice = []
    // 判断时间段
    const subDateSlice = (nowTimeStamp - toDayZeroStamp) / 3600000
    // console.log('nowTimeStamp', nowTimeStamp, 'toDayZeroStamp', toDayZeroStamp, 'n-to', (nowTimeStamp - toDayZeroStamp))
    // console.log('取整subDateSlice', Number.parseInt(subDateSlice))
    let indexData = 0
    const cutNum = subDateSlice + 0.5 - Number.parseInt(subDateSlice)
    // 5:15 + 30分钟 = 5:45 => 5:30 , cutNum = 0.75; 5:15分以后6点之前 都是 小时数+1
    // 5:16 + 30分钟 = 5:46 => 6:00 , cutNum = 0.76;
    if (cutNum < 0.75) {
      indexData = 0
    } else {
      indexData = 1
    }

    const e = []
    for (let c = 0; c < 6; c++) {
      const minutes_item2 = {
        id: c,
        value: c * 10
      }
      e.push(minutes_item2)
    }
    const nowMinutesList = dateFun.minutesSectionFun(0)

    const integerHour = Number.parseInt(subDateSlice)
    const c_index = index
    if (c_index === 0) {
      for (let i = integerHour; i < 24; i++) {
        const startData = i + indexData
        if (i === integerHour) {
          // console.log('i', i, 'integerHour', integerHour, 'nowMinutesList', nowMinutesList)
          timeSlice.push({
            'id': i - 8 + indexData,
            'value': startData,
            'child': nowMinutesList
          })
        } else {
          timeSlice.push({
            'id': i - 8 + indexData,
            'value': startData,
            'child': e
          })
        }
      }
    } else {
      for (let k = 0; k < 24; k++) {
        const startData = k
        timeSlice.push({
          'id': k - 8,
          'value': startData,
          'child': e
        })
      }
    }
    // console.log('e', e, 'nowMinutesList', nowMinutesList)
    return timeSlice
  },

  /*
		*时间列表 -最后放出的 时间表 数列 加载进 multiPicker 里供于选择
		*/
  timeList: function(_day) {
    const list = dateFun.getDateArr(_day)
    // console.log('进入初始数据timeList-list', list)
    const timeList = []
    const dayList = []
    const that = this
    list.map(function(_itm, idx) {
      if (_itm.child && _itm.child.length > 0) {
        dayList.push(_itm)
      }
    })
    console.log('列表排列中-dayList', dayList)
    dayList.map(function(_itm, idx) {
      // console.log('起-1天的数据-dayList.map-itm', _itm, 'idx', idx, '_itm.month', _itm.month,
      //   '_itm.day', _itm.day, '_itm.child', _itm.child, 'idx', idx)
      if (idx === 0) {
        timeList.push(that.monthJsonOp(_itm.month, _itm.day, _itm.child, idx))
      } else {
        // console.log('idx !== 0-_itm', _itm, 'idx', idx, '_itm.child', _itm.child)
        timeList.map(function(item, index) {
          if (_itm.month === item.id) {
            timeList.push(that.monthJsonOp(_itm.month, _itm.day, _itm.child, idx))
          } else if (index === timeList.length - 1) {
            // console.log('index', index, 'timeList.length', timeList.length, '此时除掉第天数据的item', item)
            timeList.push(that.monthJsonOp(_itm.month, _itm.day, _itm.child, idx))
          }
        })
      }
    })
    console.log('排列最后生成的-timeList', timeList)
    return timeList
  }
}
export default dateFun
// ; dd ;//
